package PagePackage;
import static org.testng.AssertJUnit.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import PagePackage.teamsPage;


public class newSchoolPage extends internalPageClass{
	@FindBy (xpath="html/body/main/section/aside/nav/ul/li/a")
	public WebElement schoolInfoTab;
	
	
	@FindBy (name="type")
	public WebElement schoolTypeDropdown;
	
	@FindBy (name="name")
	public WebElement newSchoolNameField;
	@FindBy (name="mascot")
	public WebElement schoolMascotField;
	
	@FindBy (name="conference")
	public WebElement conferenceField;
	
	
	@FindBy (name="division")
	public WebElement divisionField;
	
	@FindBy (name="address1")
	public WebElement address1Field;
	
	@FindBy (name="city")
	public WebElement cityField;
	
	@FindBy (name="country")
	public WebElement countryField;
	
	@FindBy (name="address2")
	public WebElement address2Field;
	
	@FindBy (name="state")
	public WebElement stateField;
	
	@FindBy (name="postal")
	public WebElement postalField;
	
	@FindBy (xpath="html/body/main/section/main/div/div[1]/form/div[2]/button")
	public WebElement saveButton;
	
	@FindBy (className="pull-right")
	public WebElement cancelButton;
	
	
	
	
	public newSchoolPage chooseType(String schoolType) {
		Select select1 = new Select(schoolTypeDropdown);
		 select1.selectByVisibleText(schoolType);
		 return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	
	public newSchoolPage schoolName(String schoolName) {
		 newSchoolNameField.click();
		 newSchoolNameField.clear();
		 newSchoolNameField.sendKeys(schoolName);
		 return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	public newSchoolPage mascot(String mascotName) {
		schoolMascotField.click();
		schoolMascotField.clear();
		schoolMascotField.sendKeys(mascotName);
		return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	public newSchoolPage conference(String conferenceName) {
		conferenceField.click();
		conferenceField.clear();
		conferenceField.sendKeys(conferenceName);
		return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	public newSchoolPage division(String divisionName) {
		divisionField.click();
		divisionField.clear();
		divisionField.sendKeys(divisionName);
		return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	public newSchoolPage address1(String address1Name) {
		address1Field.click();
		address1Field.clear();
		address1Field.sendKeys(address1Name);
		return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	public newSchoolPage address2 (String address2Name) {
		address2Field.click();
		address2Field.clear();
		address2Field.sendKeys(address2Name);
		return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	public newSchoolPage city (String cityName) {
		cityField.click();
		cityField.clear();
		cityField.sendKeys(cityName);
		return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	public newSchoolPage country (String countryName) {
		countryField.click();
		countryField.clear();
		countryField.sendKeys(countryName);
		return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	public newSchoolPage state (String stateName) {
		stateField.click();
		stateField.clear();
		stateField.sendKeys(stateName);
		return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	public newSchoolPage postal (String postalName) {
		postalField.click();
		postalField.clear();
		postalField.sendKeys(postalName);
		return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	
	public schoolsPage saveSchool() {
	
		saveButton.click();
		return MyPageFactory.getPage(driver, schoolsPage.class);
	}
	
	public schoolsPage cancelSchool() {
		
		cancelButton.click();
		return MyPageFactory.getPage(driver, schoolsPage.class);
	}
public newSchoolPage checkSchool() {
		
		
		
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {try {
		                if (newSchoolNameField.getText()!="");
		            }
		                
		                catch( StaleElementReferenceException e )
		                {
		                	System.out.println( "Select failed! Try again..." );
		                    return false;
		                }
		                System.out.println( "School2 is found!" );
		                return true;
		                
		                }
		} );
		
		
		
		 return MyPageFactory.getPage(driver, newSchoolPage.class);
	}
	
	
	/*
	public void checkSchool() {
	
	new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return newSchoolNameField.getText().length() != 0;
        }
    }) ;
	 return MyPageFactory.getPage(driver, newSchoolPage.class);
		}
*/
	
	
	
	
	public boolean isOnThisPage(){
		return schoolInfoTab.isDisplayed();
	}
 @Override
void tryToOpen() {
	MyPageFactory.getPage(driver, loginPageClass.class).loginAs("victoria@krossover.com", "webmaster");
	schooltab.click();
}
}